/**
 * This file is part of routerdns.
 * 
 * AUTHORS
 *     Oliver Mader  <dotb52@gmail.com>
 * 
 * Copyright (C) 2009  Oliver Mader
 * 
 * Routerdns is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Routerdns is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with routerdns.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "log.h"

#include <stdlib.h>
#include <stdio.h>

/* the main log function which writes to the syslog */
static void log_func(int priority, char *format, va_list ap)
{
	/* open the a syslog connection */
	openlog("routerdns", LOG_PID, LOG_USER);
	
	char buffer[256];
	vsnprintf(buffer, 256, format, ap);
	
	/* log the message to syslog*/
	syslog(priority, "%s", buffer);
	
	/* close the connection */
	closelog();
}

/* a helper function around log_func for errors */
extern void log_error(char *format, ...)
{
	/* the optional arguments */
	va_list ap;
	va_start(ap, format);
	
	/* log it */
	log_func(LOG_ERR, format, ap);
	vfprintf(stderr, format, ap);
	
	va_end(ap);
}

/* a helper function around log_func for normal messages */
extern void log_message(char *format, ...)
{
	/* the optional arguments */
	va_list ap;
	va_start(ap, format);
	
	/* log it */
	log_func(LOG_INFO, format, ap);
	
	va_end(ap);
}
